package com.broadsoft.platform.wfengine.runtime.nodehandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.broadsoft.platform.wfengine.NodeType;
import com.broadsoft.platform.wfengine.parser.ParsedWorkflowDefinition;
import com.broadsoft.platform.wfengine.runtime.WfContext;
import com.broadsoft.platform.wfengine.runtime.impl.NodeExecutionServiceImpl;

public class StartNodeExecutionHandler implements INodeExecutionHandler {

    private final static Logger logger = LoggerFactory.getLogger(NodeExecutionServiceImpl.class);

    public StartNodeExecutionHandler(ParsedWorkflowDefinition parsedWorkflowInfo) {
        this.parsedWorkflowInfo = parsedWorkflowInfo;

    }

    @Override
    public NodeType executeNode(NodeType currentNode, WfContext flowContext) {
        if (logger.isTraceEnabled()) {
            logger.trace("The method[executeNode] begin");
        }
        flowContext.setPreviousNode(currentNode);
        String flowName = flowContext.getWorkflowType().getName();
        String destNodeName = currentNode.getDestNode().get(0);
        NodeType nextNode = parsedWorkflowInfo.getNodeTypeByName(flowName, destNodeName);
        if (logger.isTraceEnabled()) {
            logger.trace("The local variable [nextNode] is {}.", nextNode.getName());
            logger.trace("The method[executeNode] end");
        }
        return nextNode;
    }

    private ParsedWorkflowDefinition parsedWorkflowInfo;

    public ParsedWorkflowDefinition getParsedWorkflowInfo() {
        return parsedWorkflowInfo;
    }

    public void setParsedWorkflowInfo(ParsedWorkflowDefinition parsedWorkflowInfo) {
        this.parsedWorkflowInfo = parsedWorkflowInfo;
    }

}
